/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.business;

import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;

import net.dgg.dggcloud.service.jrcrm.dao.business.BusinessDao;
import net.dgg.dggcloud.service.jrcrm.dao.business.GroupDao;
import net.dgg.dggcloud.service.jrcrm.entity.business.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName 〈GroupService>
 * @Description
 * @Author 杜倩
 * @Date 2018/1/18 11:42
 */
@Service
public class GroupService extends BaseService {

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private BusinessDao businessDao;
    /**
     * 保存分组
     *
     */
    public void saveOrUpdateGroup(String groupId,String groupName,User user) {
        try {
            Group group = null;
            if (StringUtils.isNotBlank(groupId)) {
                group = this.repository.get(Group.class, Long.parseLong(groupId));
            } else {
                group = new Group();
                group.setId(keyWorker.nextId());
                group.setCreateUser(user);
            }
            group.setGroupName(groupName);
            group.setUpdaterUser(user);

            this.repository.saveOrUpdate(group);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("添加分组失败！");
            throw new BizException("添加分组失败！",10079);
        }

    }

    public void deleteGroup(String groupId,User user){

       try {
           //清空商机的分组id

           Map m = new HashMap();
           m.put("groupId",Long.parseLong(groupId));
           businessDao.updateGroupBusiness(m);

           //删除分组
          this.repository.deleteEntityById(Group.class,Long.parseLong(groupId));
       }catch (Exception e){
           e.printStackTrace();
           logger.error("删除分组失败！");
           throw new BizException("删除分组失败！",10079);
       }


    }

    public List<Map> getGroupListAndBusCount(Map m){
        List<Map> list = groupDao.getGroupListAndBusCount(m);
        return list;
    }

    public List<Group> getGroupListByUser(User user){
        List<Group> list = this.repository.findByProperty(Group.class,"createrId",user.getId());
        return  list;
    }

    public List<Map> getGroupListByUserMap(Map map){
        List<Map> list = groupDao.getGroupListByMap(map);
        //  List<Group> list = this.repository.findByProperty(Group.class,"createrId",user.getId());
        return  list;
    }

    public Group getGroupById(Long groupId) {
        return repository.get(Group.class, groupId);
    }


    public List<Map> getGroupListAndBusCountBySigned(Map m){
        List<Map> list = groupDao.getGroupListAndBusCountBySigned(m);
        return list;
    }
}
